//
//  ZCHomeViewModel.swift
//  weibo
//
//  Created by 张城 on 16/7/23.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCHomeViewModel: NSObject {
    
    lazy var dataArray:[ZCHomeCellViewModel] = [ZCHomeCellViewModel]()
    
}


extension ZCHomeViewModel {
    
    func homeLoadData(isPull: Bool ,isSuccess:(isSuc: Bool,count: Int)->()) {
        
        var maxId: Int64 = 0
        var sinceId: Int64 = 0
        
        if isPull {
            maxId = (dataArray.last?.homeModel?.id) ?? 0
            if maxId > 0 {
                maxId -= 1
            }
            
        }else{
            sinceId = (dataArray.first?.homeModel?.id) ?? 0
        }
        
        ZCNetworkTools.sharedTool.homeLoadUserDetailInfo(sinceId, maxId: maxId, success: { (response) in
            guard let result = response?["statuses"] as? [[String : AnyObject]] else {
                
                
//                isSuccess(isSuc: false)
                isSuccess(isSuc: false, count: 0)
                
                return
            }
            
            var tempArr:[ZCHomeCellViewModel] = [ZCHomeCellViewModel]()
            
            for dics in result {
                let homeModel = ZCHomeModel(dic: dics)
                
                let statusViewModel = ZCHomeCellViewModel(homeModel: homeModel)
                tempArr.append(statusViewModel)
            }
            
            
            if isPull {
                self.dataArray = self.dataArray + tempArr
                isSuccess(isSuc: true, count: -1)
            }else{
                self.dataArray = tempArr + self.dataArray
                isSuccess(isSuc: true, count: tempArr.count)
            }
            
//            isSuccess(isSuc: true)
            
            
            
        }) { (error) in
            
            isSuccess(isSuc: false, count: 0)
//            isSuccess(isSuc: false)
        }
        
    }
    
    
}